package nina.yiyou.salary.input

import nina.yiyou.salary.input.util.InputPathResolver

import java.nio.file.Files
import java.nio.file.Path

class InfoExcelCollector {

  InfoExcelCollector(List<List> infoMeta, Map globalMap, Path workshopPath) {
    _infoMeta = infoMeta
    _globalMap = globalMap
    _workshopPath = workshopPath
  }

  List collect() {
    def resolvePath = { String path -> new InputPathResolver(_workshopPath, path).resolve() }

    return _infoMeta.collect { String key, Closure<Map> reader ->
      assert _globalMap[key]

      Path excelPath = resolvePath(_globalMap[key])
      print("读取<$key>：${excelPath}，人数：")
      assert Files.isRegularFile(excelPath)

      Map result = reader(excelPath.toString())
          .findAll { k, v -> k }
          .collectEntries { k, v -> [k, v] }

      println(result.size())
      return result
    }
  }

  private final List<List> _infoMeta

  private final Map<String, String> _globalMap
  private final Path _workshopPath
}
